﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;

namespace ManagerVerification
{
    public partial class BackDoor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebUser.UserName == "Administrator")
            {
                AdminLoginPanel.Visible = false;
                AdminTools.Visible = true;

                AddManager_Error.Text = "";
            }
            else
            {
                AdminLoginPanel.Visible = true;                
                AdminTools.Visible = false;
            }
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            //SHA256 ShaM0 = new SHA256Managed();
            //byte[] encPassword0 = ShaM0.ComputeHash(Encoding.ASCII.GetBytes("123123"));

            //using (var ta = WebUser.SET_ManagerPassword)
            //using (var table = ta.GetData(Guid.Parse("ccec4385-456b-46c9-982a-b754f050c94f"),encPassword0))
            //{
            //}

            if (!string.IsNullOrEmpty(Password.Text))
            {
                SHA256 ShaM = new SHA256Managed();
                byte[] encPassword = ShaM.ComputeHash(Encoding.ASCII.GetBytes(Password.Text));

                using (var ta = WebUser.GET_AdminVerification)
                using (var table = ta.GetData(encPassword))
                {
                    if (table.Rows.Count > 0)
                    {
                        if (bool.Parse(table.Rows[0]["Result"].ToString()))
                        {
                            WebUser.UserName = "Administrator";



                            Response.Redirect(Request.RawUrl);
                        }
                    }
                }
            }
        }

        protected void AddManager_Add_Click(object sender, EventArgs e)
        {
            string pass = AddManager_Pwd.Text;
            string name = AddManager_Name.Text;

            if (string.IsNullOrEmpty(pass) || string.IsNullOrEmpty(name))
            {
                AddManager_Error.Text = "Пустые поля";
            }
            else
            {
                SHA256 ShaM = new SHA256Managed();
                byte[] encPassword = ShaM.ComputeHash(Encoding.ASCII.GetBytes(pass));

                using (var ta = WebUser.ADD_ManagerAccount)
                using (var table = ta.GetData(name, encPassword))
                {
                    AddManager_Error.Text = "Менеджер добавлен";
                }
            }
        }
    }
}